package com.tkx.dxb.service.oss.service.impl;

import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.aliyun.oss.model.CannedAccessControlList;
import com.aliyun.oss.model.DeleteObjectsRequest;
import com.tkx.dxb.service.oss.service.FileService;
import com.tkx.dxb.service.oss.util.OssProperties;
import org.joda.time.DateTime;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

/**
 * @author : victor
 * @since : 2020/11/2
 */
@Service
public class FileServiceImpl implements FileService {

    private OssProperties ossProperties;

    @Autowired
    public void setOssProperties(OssProperties ossProperties){
        this.ossProperties = ossProperties;
    }

    @Override
    public String upload(InputStream inputStream, String module, String originalFilename) {

        String endpoint = ossProperties.getEndpoint();
        String keyId = ossProperties.getKeyId();
        String keySecret = ossProperties.getKeySecret();
        String bucketName = ossProperties.getBucketName();

        // 创建OSSClient实例。
        OSS ossClient = new OSSClientBuilder().build(endpoint, keyId, keySecret);
        //判断是否存在bucketName实例
        if (!ossClient.doesBucketExist(bucketName)){
            //创建一个新的bucketName
            ossClient.createBucket(bucketName);
            //设置为公共读
            ossClient.setBucketAcl(bucketName, CannedAccessControlList.PublicRead);
        }
        
        //构建bucketName：文件路径 avatar/2020/11/2/default.jpg
        String folder = new DateTime().toString("yyyy/MM/dd");
        String fileName = UUID.randomUUID().toString();
        String fileExtension = originalFilename.substring(originalFilename.lastIndexOf("."));
        String key = module + "/" + folder + fileName + "." + fileExtension;

        // 上传文件流
        ossClient.putObject(bucketName, key, inputStream);

        // 关闭OSSClient
        ossClient.shutdown();

        //https://dxbedu.oss-cn-chengdu.aliyuncs.com/avatar/default.jpg
        return "https://" + bucketName + "." + endpoint + "/" + key;
    }

    @Override
    public void removeFile(String url) {

        String endpoint = ossProperties.getEndpoint();
        String keyId = ossProperties.getKeyId();
        String keySecret = ossProperties.getKeySecret();
        String bucketName = ossProperties.getBucketName();

        // 创建OSSClient实例。
        OSS ossClient = new OSSClientBuilder().build(endpoint, keyId, keySecret);

        String host = "https://" + bucketName + "." + endpoint + "/";
        String objectName = url.substring(host.length());

        // 删除文件
        ossClient.deleteObject(bucketName, objectName);

        // 关闭OSSClient
        ossClient.shutdown();
    }

    @Override
    public void removeFiles(List<String> urls) {

        String endpoint = ossProperties.getEndpoint();
        String keyId = ossProperties.getKeyId();
        String keySecret = ossProperties.getKeySecret();
        String bucketName = ossProperties.getBucketName();

        // 创建OSSClient实例。
        OSS ossClient = new OSSClientBuilder().build(endpoint, keyId, keySecret);

        List<String> keys = new ArrayList<String>();
        for(String url : urls) {
            String host = "https://" + bucketName + "." + endpoint + "/";
            String key = url.substring(host.length());
            keys.add(key);
        }
        ossClient.deleteObjects(new DeleteObjectsRequest(bucketName).withKeys(keys));

        // 关闭OSSClient。
        ossClient.shutdown();
    }
}
